﻿using Azure.Core;
using Che.Domain.Entity.EPlayers;
using Che.Domain.Entity.Fans;
using Che.Domain.repository.RoleJue;
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.Domain.Entity.rbac_QXGL;
namespace Che.EFCore.Repository.EFRolejue
{
    public class EFRolejue1:IRolejue
    {
        private readonly MyDbContext myContext;
        private readonly string _uploadPath;
        public EFRolejue1(MyDbContext myContext )
        {
            this.myContext = myContext;
           
        }

        public async Task<List<CeBianlan>> CeBianlanSelectAsync(string jid)
        {
            using (SqlConnection connection = new SqlConnection(myContext.conStr))
            {
                string sql = $@"select  qx.u_juriid,u_jurname,u_juraddress,u_jurpid,U_rolesiD
                                from rbac_RolesJurisdiction as jsqx
                                inner join rbac_Jurisdiction as qx
                                on qx.u_juriid=jsqx.U_juriiD
                                where U_rolesiD={jid} and u_jurpid=0
                                order by qx.U_juriiD asc";
                List<CeBianlan> list = (List<CeBianlan>)await connection.QueryAsync<CeBianlan>(sql);
                foreach (CeBianlan item in list)
                {
                    string sql1 = $@"select  qx.u_juriid,u_jurname,u_juraddress,u_jurpid,U_rolesiD
                                from rbac_RolesJurisdiction as jsqx
                                inner join rbac_Jurisdiction as qx
                                on qx.u_juriid=jsqx.U_juriiD
                                where U_rolesiD={jid} and u_jurpid={item.u_juriid}
                                order by qx.U_juriiD asc";
                    item.ulist = (List<CeBianlan>)await connection.QueryAsync<CeBianlan>(sql1);
                }
                return list;
            }
        }

        public async Task<rbac_UserRoles> queryU_rolesiD(int U_rolesiD)
        {
            return await myContext.rbac_UserRoles
        .FirstOrDefaultAsync(ur => ur.U_useriD == U_rolesiD);
        }

        public void serverTable()
        {
            var db = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = "Data Source=CHELSEA;Initial Catalog=ChelseaFlootball2;Integrated Security=True;Trust Server Certificate=True;Encrypt=False;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            });

            // 修正1：正确获取程序集类型
            Type[] entityTypes = typeof(pansintalis).Assembly.GetTypes()
         .Where(it => it.Namespace == "Che.Domain.Entity.Fans") // 精确匹配命名空间
         .ToArray();

            // 修正2：添加表创建日志
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql); // 打印生成的SQL
            };

            // 配置并创建表
            db.CodeFirst
              .SetStringDefaultLength(200)
              .InitTables(entityTypes);

            Console.WriteLine("表创建完成！");
            Console.ReadKey();
        }

        public async Task<Domain.Entity.Roleju.FileInfo> UploadAsync(IFormFile file)
        {
            if (file == null || file.Length == 0)
                throw new ArgumentException("文件不能为空");

            // 生成唯一文件名
            var fileExtension = Path.GetExtension(file.FileName);
            var storedFileName = $"{Guid.NewGuid()}{fileExtension}";
            var filePath = Path.Combine("D:\\chelsea项目\\chelseafootball\\src\\assets", storedFileName);

            // 保存文件
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return new Domain.Entity.Roleju.FileInfo(
                file.FileName,
                storedFileName,
                file.ContentType,
                file.Length,
                filePath);
        }
    }
}
